package linklist;

public class L206 {
    public static ListNode reverseList(ListNode head) {
        ListNode temp=null;
        ListNode cur=head;
        ListNode pre=null;
        while (cur!= null){
            temp=cur.next;
            cur.next=pre;
            pre=cur;
            cur=temp;

        }
        return pre;



    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(4);
        head.next.next.next.next = new ListNode(5);

        // 反转链表
        ListNode reversedList = reverseList(head);

        // 打印反转后的链表
        while (reversedList != null) {
            System.out.print(reversedList.val + " ");
            reversedList = reversedList.next;
        }
    }



}
